package com.example.mobile2021606170hsy0328homework;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity implements  View.OnClickListener {

    private TextView tvMainTitle;
    private EditText edtTxtMainShow;
    private Button btnMainBack;
    private Button btnMainDiv;
    private Button btnMainCld;
    private Button btnMainNum7;
    private Button btnMainNum8;
    private Button btnMainNum9;
    private Button btnMainSub;
    private Button btnMainNum4;
    private Button btnMainNum5;
    private Button btnMainNum6;
    private Button btnMainPersent;
    private Button btnMainNum1;
    private Button btnMainNum2;
    private Button btnMainNum3;
    private Button btnMainAdd;
    private Button btnMainNum0;
    private Button btnMainPoint;
    private Button btnMainEqu;

    String msga, msgb;
    int a, b;

    boolean clear_flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainNum0.setOnClickListener(this);
        btnMainNum1.setOnClickListener(this);
        btnMainNum2.setOnClickListener(this);
        btnMainNum3.setOnClickListener(this);
        btnMainNum4.setOnClickListener(this);
        btnMainNum5.setOnClickListener(this);
        btnMainNum6.setOnClickListener(this);
        btnMainNum7.setOnClickListener(this);
        btnMainNum8.setOnClickListener(this);
        btnMainNum9.setOnClickListener(this);
        btnMainCld.setOnClickListener(this);
        btnMainBack.setOnClickListener(this);
        btnMainAdd.setOnClickListener(this);
        btnMainEqu.setOnClickListener(this);
        btnMainDiv.setOnClickListener(this);
        btnMainSub.setOnClickListener(this);
        btnMainPersent.setOnClickListener(this);
        btnMainPoint.setOnClickListener(this);
    }

    private void initView() {
        tvMainTitle = findViewById(R.id.tv_main_title);
        edtTxtMainShow = findViewById(R.id.edtTxt_main_show);
        btnMainBack = findViewById(R.id.btn_main_back);
        btnMainDiv = findViewById(R.id.btn_main_div);
        btnMainCld = findViewById(R.id.btn_main_cld);
        btnMainNum7 = findViewById(R.id.btn_main_num7);
        btnMainNum8 = findViewById(R.id.btn_main_num8);
        btnMainNum9 = findViewById(R.id.btn_main_num9);
        btnMainSub = findViewById(R.id.btn_main_sub);
        btnMainNum4 = findViewById(R.id.btn_main_num4);
        btnMainNum5 = findViewById(R.id.btn_main_num5);
        btnMainNum6 = findViewById(R.id.btn_main_num6);
        btnMainPersent = findViewById(R.id.btn_main_persent);
        btnMainNum1 = findViewById(R.id.btn_main_num1);
        btnMainNum2 = findViewById(R.id.btn_main_num2);
        btnMainNum3 = findViewById(R.id.btn_main_num3);
        btnMainAdd = findViewById(R.id.btn_main_add);
        btnMainNum0 = findViewById(R.id.btn_main_num0);
        btnMainPoint = findViewById(R.id.btn_main_point);
        btnMainEqu = findViewById(R.id.btn_main_equ);

    }

    @Override
    public void onClick(View v) {
        Button btn = (Button) v;
        int id = btn.getId();
        String input = edtTxtMainShow.getText().toString();
        switch (id) {
            case R.id.btn_main_num0:
                edtTxtMainShow.append("0");
                break;
            case R.id.btn_main_num1:
                edtTxtMainShow.append("1");
                break;
            case R.id.btn_main_num2:
                edtTxtMainShow.append("2");
                break;
            case R.id.btn_main_num3:
                edtTxtMainShow.append("3");
                break;
            case R.id.btn_main_num4:
                edtTxtMainShow.append("4");
                break;
            case R.id.btn_main_num5:
                edtTxtMainShow.append("5");
                break;
            case R.id.btn_main_num6:
                edtTxtMainShow.append("6");
                break;
            case R.id.btn_main_num7:
                edtTxtMainShow.append("7");
                break;
            case R.id.btn_main_num8:
                edtTxtMainShow.append("8");
                break;
            case R.id.btn_main_num9:
                edtTxtMainShow.append("9");
                break;
            case R.id.btn_main_point:
                if (clear_flag) {
                    clear_flag = false;
                    edtTxtMainShow.setText("");
                }
                edtTxtMainShow.setText(input + ((Button) v).getText());
                break;
            case R.id.btn_main_cld:
                // extTextMainShow.setText(null);
                if (clear_flag) {
                    clear_flag = false;
                    input = "";
                    edtTxtMainShow.setText("");
                } else if (input != null || !input.equals("")) {
                    edtTxtMainShow.setText(input.substring(0, input.length() - 1));
                }
                break;
            case R.id.btn_main_back:
                String msg;
                edtTxtMainShow.getText().toString();
                int length = msga.length();
                if (Pattern.matches("^=[0-9].*", msga)) {
                    edtTxtMainShow.setText("0");
                } else {
                    //if (msg.length() > 1) {
                    // msg.substring(0, msg.length());
                    if (length > 0) {
                        String word = msga.substring(length - 1, length);
                        if (word.equals(".")) ;
                        if (word.equals("+") || word.equals("-") || word.equals("*") || word.equals(("/")))
                            edtTxtMainShow.setText(msga.substring(0, length - 1));
                    }
                }

                break;
            case R.id.btn_main_add:
                msga = edtTxtMainShow.getText().toString();
                int a = Integer.valueOf(msga);
                edtTxtMainShow.append("+");
                break;


            case R.id.btn_main_div:
                // extTextMainShow.append("/");
                if (clear_flag) {
                    clear_flag = false;
                    input = "";
                    edtTxtMainShow.setText("");
                }
                edtTxtMainShow.setText(input + " " + ((Button) v).getText() + " ");
                break;
            case R.id.btn_main_mul:
                edtTxtMainShow.append("*");
                break;
            case R.id.btn_main_sub:
                edtTxtMainShow.append("-");
                break;
            case R.id.btn_main_equ:
                edtTxtMainShow.append("=");
                getResult();
                break;
        }
    }

    private void getResult() {
        String exp = edtTxtMainShow.getText().toString();
        if (exp == null || exp.equals("")) {
            return;
        }
        if (!exp.contains(" ")) {
            return;
        }
        if (clear_flag) {
            clear_flag = false;
            return;
        }
        clear_flag = true;
        double result = 0;
        String s1 = exp.substring(0, exp.indexOf(" "));
        String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2);
        String s2 = exp.substring(exp.indexOf(" ") + 3);

        if (!s1.equals(" ") && !s2.equals("")) {
            double d1 = Double.parseDouble(s1);
            double d2 = Double.parseDouble(s2);
            if (op.equals("+")) {
                result = d1 + d2;
            } else if (op.equals("-")) {
                result = d1 - d2;
            } else if (op.equals("*")) {
                if (d2 == 0) {
                    result = 0;
                } else {
                    result = d1 / d2;
                }
            }
            if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {
                int r = (int) result;
                edtTxtMainShow.setText(r + "");
            } else {
                edtTxtMainShow.setText(result + "");
            }
        }
        if (!s1.equals("") && s2.equals("")) {
            edtTxtMainShow.setText(exp);
        } else if (s1.equals("") && !s2.equals("")) {
            double d2 = Double.parseDouble(s2);
            if (op.equals("+")) {
                result = 0 + d2;
            } else if (op.equals("-")) {
                result = 0 - d2;
            } else if (op.equals("*")) {
                result = 0;
            } else if (op.equals("/")) {
                if (!s1.contains(".") && !s2.contains(".")) {
                    int r = (int) result;
                    edtTxtMainShow.setText(r + "");
                } else {
                    edtTxtMainShow.setText(result + "");
                }
            } else {
                edtTxtMainShow.setText("");

            }
        }


        }


    }
